package com.nuoniu.sibanyun.service.expenses.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.crm.CrmGoodsOrder;
import com.nuoniu.sibanyun.entity.expenses.ExpensesAccount;
import com.nuoniu.sibanyun.entity.expenses.ExpensesBalanceChangeRecord;
import com.nuoniu.sibanyun.entity.expenses.ExpensesBusinessOrder;
import com.nuoniu.sibanyun.entity.expenses.dto.ExpensesBalanceChangeRecordSearchDto;
import com.nuoniu.sibanyun.mapper.expenses.ExpensesBalanceChangeRecordMapper;
import com.nuoniu.sibanyun.service.expenses.IExpensesAccountService;
import com.nuoniu.sibanyun.service.expenses.IExpensesBalanceChangeRecordService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 费用中心-收支明细 服务实现类
 * </p>
 *
 * @author 白强
 * @since 2022-05-06
 */
@Service
@Log4j2
public class ExpensesBalanceChangeRecordServiceImpl extends ServiceImpl<ExpensesBalanceChangeRecordMapper, ExpensesBalanceChangeRecord> implements IExpensesBalanceChangeRecordService {

    @Autowired
    @Lazy
    private IExpensesAccountService expensesAccountService;

    @Override
    public Page<ExpensesBalanceChangeRecord> pageList(ExpensesBalanceChangeRecordSearchDto changeRecordSearchDto) {
        UserInfo userInfo = UserThreadLocal.get();
        Integer companyId = userInfo.getCompanyId();
        String transactionTimeStart = changeRecordSearchDto.getTransactionTimeStart();
        String transactionTimeEnd = changeRecordSearchDto.getTransactionTimeEnd();
        String transactionType = changeRecordSearchDto.getTransactionType();
        String sourceOrderNo = changeRecordSearchDto.getSourceOrderNo();
        String transactionChannel = changeRecordSearchDto.getTransactionChannel();
        QueryWrapper<ExpensesBalanceChangeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        if(StringUtils.isNotBlank(transactionTimeStart)){
            queryWrapper.ge("transaction_time",transactionTimeStart);
        }
        if(StringUtils.isNotBlank(transactionTimeEnd)){
            queryWrapper.le("transaction_time",transactionTimeEnd);
        }
        if(StringUtils.isNotBlank(transactionType)){
            queryWrapper.eq("transaction_type",transactionType);
        }
        if(StringUtils.isNotBlank(sourceOrderNo)){
            queryWrapper.like("source_order_no",sourceOrderNo);
        }
        if(StringUtils.isNotBlank(transactionChannel)){
            queryWrapper.eq("transaction_channel",transactionChannel);
        }
        queryWrapper.orderByDesc("id");
        Page page = page(changeRecordSearchDto.getPage(), queryWrapper);
        return page;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void addRecord(ExpensesBalanceChangeRecord changeRecord) {
        Integer companyId = changeRecord.getCompanyId();
        Integer serialNum = this.getSerialNum(companyId);
        changeRecord.setSerialNum(serialNum);
        changeRecord.setCompanyId(companyId);
        int count = this.checkRecord(changeRecord);
        if(count > 0) {
            throw new NuoNiuException("交易已完成");
        }
        save(changeRecord);
    }

    @Override
    public int checkRecord(ExpensesBalanceChangeRecord changeRecord) {
        Integer companyId = changeRecord.getCompanyId();
        String transactionType = changeRecord.getTransactionType();
        String sourceOrderNo = changeRecord.getSourceOrderNo();
        QueryWrapper<ExpensesBalanceChangeRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("company_id",companyId);
        queryWrapper.eq("transaction_type",transactionType);
        queryWrapper.eq("source_order_no",sourceOrderNo);
        return count(queryWrapper);
    }

    @Override
    public Integer getSerialNum(Integer companyId) {
        Integer serialNum = baseMapper.getSerialNum(companyId);
        if(null == serialNum) {
            serialNum = 0;
        }
        serialNum ++;
        return serialNum;
    }


 }
